package com.gec.wiki.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.wiki.exception.BusinessException;
import com.gec.wiki.exception.BusinessExceptionCode;
import com.gec.wiki.pojo.Doc;
import com.gec.wiki.pojo.Doc;
import com.gec.wiki.pojo.req.DocQueryReq;
import com.gec.wiki.pojo.resp.DocQueryResp;
import com.gec.wiki.pojo.resp.PageResp;
import com.gec.wiki.service.DocService;
import com.gec.wiki.mapper.DocMapper;
import com.gec.wiki.utils.CopyUtil;
import com.gec.wiki.utils.RedisUtil;
import com.gec.wiki.utils.RequestContext;
import com.gec.wiki.websocket.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
* @author CGH
* @description 针对表【doc】的数据库操作Service实现
* @createDate 2022-12-22 09:09:32
*/
@Service
public class DocServiceImpl extends ServiceImpl<DocMapper, Doc>
    implements DocService{

    //打印日志
    private static final Logger LOG = LoggerFactory.getLogger(Doc.class);

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private WsService wsService;

    @Override
    public PageResp<DocQueryResp> getDocListByPage(DocQueryReq req) {
        Page<Doc> page = new Page<>(req.getPage(), req.getSize());
        page = this.page(page);

        LOG.info("总行数 {}",page.getTotal()+"");
        LOG.info("总页数 {}",page.getPages()+"");

        List<DocQueryResp> resps = CopyUtil.copyList(page.getRecords(), DocQueryResp.class);
        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setList(resps);
        pageResp.setTotal(page.getTotal());
        return pageResp;
    }

    @Override
    public List<DocQueryResp> allList(DocQueryReq req) {
        QueryWrapper<Doc> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        wrapper.eq(!ObjectUtils.isEmpty(req.getEbookId()),"ebook_id",req.getEbookId());
        List<Doc> list = this.list(wrapper);

        List<DocQueryResp> resp = CopyUtil.copyList(list, DocQueryResp.class);
        return resp;
    }

    @Override
    public int increaseViewCount(int id) {
        return this.baseMapper.increaseViewCount(id);
    }

    @Override
    public int increaseVoteCount(int id) {
        //远程IP+doc.id作为key，24小时不能重复点赞
        String ip = RequestContext.getRemoteAddr();
        Doc docDB = this.baseMapper.selectById(id);

        String logId = MDC.get("LOG_ID");

        if (redisUtil.validateRepeat("DOC_VOTE_"+id+"_"+ip,3600*24)){
            wsService.sendInfo("【"+docDB.getName()+"】被点赞了！",logId);
            return this.baseMapper.increaseVoteCount(id);
        } else {
            wsService.sendInfo("【"+docDB.getName()+"】这篇文章您已经点赞过了！",logId);
            throw new BusinessException(BusinessExceptionCode.VOTE_REPEAT);
        }
    }

    @Override
    public int updateEbookInfo() {
        return this.baseMapper.updateEbookInfo();
    }
}





